package com.swyc.osschat.Test.StreamTest.Controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.swyc.osschat.Test.LambdaTest.Bean.Student;

/**
 * @author zhaol@yunrong.cn
 * @version V2.1
 * @since 2.1.0 2019/8/9 16:09
 */
public class TestCase {
    public static void main(String[] args) {
        //todo 将流转换为list。还有toSet()，toMap()等。及早求值。
        /*List<Student> studentList = Stream.of(
            new Student("路飞", 22, 175),
            new Student("红发", 40, 180),
            new Student("白胡子", 50, 185)).collect(Collectors.toList());*/
        List<Student> studentList = new ArrayList<>(3);
        studentList.add(new Student("路飞", 22, 175));
        studentList.add(new Student("红发", 22, 180));
        studentList.add(new Student("白胡子", 50, 185));
        //todo filter筛选出出身高小于180的同学。
        List<Student> list = studentList.stream()
            .filter(stu -> stu.getStature() < 180)
            .collect(Collectors.toList());
        //todo 转换功能，内部就是Function接口。惰性求值
        List<String> names = studentList.stream().map(Student::getName)
            .collect(Collectors.toList());
        //todo 将多个Stream合并为一个Stream。惰性求值
        List<Student> students = Stream.of(studentList,
            Arrays.asList(new Student("艾斯", 25, 183),
                new Student("雷利", 48, 176)))
            .flatMap(students1 -> students1.stream()).collect(Collectors.toList());
        //todo max和min  这个方法只会获取第一个符合条件的对象
        Optional<Student> max = students.stream()
            .max(Comparator.comparing(stu -> stu.getAge()));
        Optional<Student> min = students.stream()
            .min(Comparator.comparing(stu -> stu.getAge()));
        //判断是否有值
        if (max.isPresent()) {
            System.out.println(max.get());
        }
        if (min.isPresent()) {
            System.out.println(min.get());
        }
        //todo 统计功能，一般都是结合filter使用，因为先筛选出我们需要的再统计即可。及早求值
        long count = students.stream().filter(s1 -> s1.getAge() < 45).count();
        System.out.println("年龄小于45岁的人数是：" + count);
        System.out.println(studentList);
    }
}
